package com.app.mymvp.business.main;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.app.base.okhttp.callback.ResultCallback;
import com.app.base.okhttp.request.OkHttpGetRequest;
import com.app.base.okhttp.request.OkHttpRequest;
import com.app.mymvp.R;
import com.app.mymvp.adapter.AdPagerAdapter;
import com.app.mymvp.base.BaseFragment;
import com.app.mymvp.entity.HomeImageEntity;
import com.app.mymvp.utils.RequestLinks;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.app.mymvp.R.id.vp_home;


/**
 * Created by HJ on 2016/7/8
 * 首页标签页
 */
public class HomeFragment extends BaseFragment {

    private final int PAGER_INTERVAL = 5000;//间隔时间
    @BindView(R.id.vp_home)
    ViewPager vpHome;
    private AdPagerAdapter adPagerAdapter;
    private List<View> imagelist = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ButterKnife.bind(this, super.onCreateView(inflater, container, savedInstanceState));
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView() {
        if (isFirstRun) {
            getImgList();
        }
    }

    /**
     * 获取首页轮播图
     */
    private void getImgList() {
        new OkHttpGetRequest.Builder().url(RequestLinks.HOME_BANNER).get(new ResultCallback<List<HomeImageEntity>>(activity, true) {
            @Override
            public void onResponse(List<HomeImageEntity> response) {
                for (int i = 0; i < response.size(); i++) {
                    ImageView view = new ImageView(activity);
                    view.setScaleType(ImageView.ScaleType.FIT_XY);
                    Glide.with(activity).load("http://static.uxuexi.com/" + response.get(i).getImgurl()).fitCenter().into(view);
                    imagelist.add(view);
                }
                adPagerAdapter = new AdPagerAdapter(activity, response, imagelist);
                vpHome.setAdapter(adPagerAdapter);
            }

            @Override
            public void onStatus(String response) {
            }
        });
    }

    @Override
    public boolean onBackPressed() {
        return false;
    }

}
